# -*- coding: utf-8 -*-
# @Author: Sparks_lz
# @Date:   2018-08-15 14:53:37
# @Last Modified by:   Sparks_lz
# @Last Modified time: 2018-08-21 16:21:56


print "helloworld!"  

import os  
import sys  
import platform  

def exportCamera(fileList, path) :  
    #------------------- import modules ---------------    
    import maya.standalone as standalone  
    standalone.initialize()  
    import pymel.internal.startup  
    import pymel.core as pmc  
    import maya.cmds as cmds  

    if platform.system() == "Linux" :  
        os.environ['JIUQIAN_PROJECT'] = '/mnt/jiuqian/project/film'
    else :  
        os.environ['JIUQIAN_PROJECT'] = 'Z:/project/film'

    #------------------- load plugins ---------------
    pmc.loadPlugin('AbcBullet')  
    pmc.loadPlugin('AbcExport')  
    pmc.loadPlugin('AbcImport')  
    X = os.sep  
    basepath = path  

    #------------------- maya file looping from listwidget ---------------
    for file in fileList :  
        filepath = file
        X = os.sep
        cmds.file(filepath, f=1 , o=1, type='mayaBinary', ignoreVersion=1 , options='v=0;')
        parent_path = os.path.dirname(filepath)
        parent_name = os.path.basename(parent_path)
        start = int(pmc.playbackOptions(q=1,min=1))
        end = int(pmc.playbackOptions(q=1,max=1))


    #------------------- get ani camera ---------------
        blacklist = ["frontShape", 
                     "perspShape", 
                     "sideShape", 
                     "topShape"]
        cam_list = [cam for cam in pmc.ls(type="camera") if cam not in blacklist]
        camera_attr = ["translateX",
                       "translateY",
                       "translateZ",
                       "rotateX",
                       "rotateY",
                       "rotateZ",
                       "scaleX",
                       "scaleY",
                       "scaleZ"]
        for cam in cam_list :  
            trans_node = cam.getTransform()
            origin_cam = trans_node

    #------------------- set 3dAttr to normal ---------------                   
            for i in camera_attr:
                camAttr = pmc.Attribute("%s.%s" % (origin_cam.name(), i))
                camAttr.showInChannelBox(True)
                if camAttr.isLocked():
                    camAttr.unlock()
                else :
                    pass

    #------------------- dulicating camera node to world hierachy ---------------
            new_cam = pmc.duplicate(origin_cam, rr=True)[0]
            print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2"
            print new_cam
            try :
                pmc.parent(new_cam, world=True)
            except :
                pass

    #------------------- origin_cam ---> new_cam ---------------
            new_camera_pc = pmc.parentConstraint(origin_cam, new_cam, mo=True, weight=1)
            for attr in camera_attr :
                pmc.bakeResults("%s.%s" % (new_cam.name(), i), t=(start, end), simulation=True, sb=1)
                pmc.delete(pmc.PyNode(new_camera_pc))

    #------------------- set command string ---------------
            root = "-root %s " % (new_cam.longName())
            nnnname = str(pmc.sceneName().basename().split("_")[0])
            output_dir = basepath + X + parent_name
            output_path = output_dir + X + new_cam.name() + ".abc"
            command = "-frameRange " + str(start) + " " + str(end) + " "  + root + " -dataFormat ogawa -file " + output_path
            filepath = os.path.dirname(output_path)
            if os.path.exists(filepath) :
                cmds.AbcExport ( j = command )
            else :
                print output_path
                os.makedirs(filepath)
                cmds.AbcExport ( j = command )
            pmc.select(new_cam)
            ma_export_path = os.path.join(output_dir, "%s.mb"%(new_cam))
            cmds.file( str(ma_export_path),
                       force=True, 
                       exportSelected=True, 
                       type='mayaBinary',  
                       options='v=0;')